

var express = require('express');
var app = express();
var fs = require('fs');

var superagent = require('superagent');
var info = {mobile: "17381912715", password: "kd17381912715", verCode: "", platformType: 2, type: 2, openId: ""};
var login_url = 'https://mecs.fleetingpower.com/services/v1/broker/login'

var get_url = 'https://mecs.fleetingpower.com/services/v3/gaiaMobile/car/query';//登录后想要操作的页面或接口
 
let login = ()=>{//先登陆
return new Promise((resolve,reject)=>{
superagent
.post(login_url)
.set('Content-Type','application/json;charset=UTF-8')
.set('origin','https://mp-merchant.fleetingpower.com')
.set('user-agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36')
.set('x-app-key','broker_portal')
.set('sign','83bcb2963ecb6b3fc602089f08eba9e2')
.set('authority','mecs.fleetingpower.com')
.set('path','/services/v1/broker/login')
.set('scheme','https')
.set('x-perm-id','06A2FA521E644BBCB5640C0FE24785D1')
.send(info)
.end((err,response)=>{
err && reject(err);
var cookie = JSON.stringify(response.header['set-cookie']).trim().split(';')[0].substring(2);

var res= {}
var userinfo = JSON.parse(response.text)
res = {...userinfo.result, cookie: cookie, xPermId: response.headers['x-perm-id']}
fs.writeFile('./files/token.txt', res.token,'utf8', function(err){
    console.log('err', err)
  })
  fs.writeFile('./files/xPermId.txt', res.xPermId,'utf8', function(err){
    console.log('err', err)
  })
// console.log('cookie',res)
resolve(res);
// console.log('response.text', JSON.parse(response.text));//接口返回信息,是否登录成功！
})
})
}

login()
.then((resonse)=>{
  // console.log('app response', resonse.text)
  return resonse.text
},(err)=>{
   return err
})


